<?php
/*
Plugin Name: WordPress Simple Backup
Plugin URI: http://mifunny.info/
Description: 简单的 WordPress 备份程序。
Version: 0.1
Author: LD King
Author URI: http://mifunny.info/
License: GPL v3
*/

/**
 * @filename wp-simple-backup.php
 * @author LD King
 * @encoding UTF-8
 * @date 2008-9-11 | 下午08:20:07
 * @package wp-simple-backup
 * @subpackage 
 * @usage 
 * @note wp备份主程序
 * @copyright Copyright (c) 2007 - 2008 LD (http://mifunny.info)
 * @version $Id: $
 */


function wpSimplayBackupDisplay($msg) {
	echo $msg.'<br />'.ABSPATH.'<br />';
	
	$sss = new simpleBackup();
	$to = 'kldscs@gmail.com';
	$sub = '测试注意！！';
	$message = 'djasojdasudhwpyrpufa';
	$disklfile = array();
	var_dump($sss->sendMail($to, $sub, $message, $disklfile) );
	/*$zip = new ZipArchive();
	if ($zip->open(WP_CONTENT_DIR.'/backup-8126d/12345.zip', ZipArchive::OVERWRITE) === TRUE) {
		$wpPath = ABSPATH;
		$exclude = array(WP_CONTENT_DIR.'/backup-8126d','/home/http/mifunny/uploads');
		$zip = $sss->addFileToZip($zip, $wpPath, $exclude);
	}
	$zip->close();*/
}

/**
 * 简单的WP备份类
 *
 */
class simpleBackup {
	
	
	public function __construct() {
		add_action('admin_menu',array(&$this,'admin_meun'));
		//add_action('phpmailer_init', array(&$this, 'sendMail'));
	}
	
	function admin_meun() {
		add_management_page('Simple Backup', 'Simple Backup', 9, 'wp-simple-backup', array(&$this,'admin'));
	}

	/**
	 * 把目录打包成 .zip , 文件形式: date('Ymd-His')
	 * 
	 * @param string $savePath
	 * @param string $backupPath
	 * @param array $excldue
	 * @return string $filename
	 */
	public function buildZip( $savePath, $backupPath, $excldue) {
		$filename = $savePath.'/wp-'.date('Ymd-His').'.zip';
		$zip = new ZipArchive();
		if ($zip->open($filename, ZipArchive::OVERWRITE) === TRUE) {
			$zip = $this->addFileToZip($zip, $backupPath, $excldue);
			$zip->close();
			return $filename;
		}else {
			$zip->close();
			return null;
		}
	}/* END func buildZip */
	
	/**
	 * 把文件添加到 .zip 压缩档案中
	 * 
	 * @package ZipArchive $zip
	 * @package string $wpPath
	 * @package array $exclude
	 * @return ZipArchive
	 */
	public function addFileToZip(ZipArchive $zip, $wpPath, $exclude) {
		
		/**
		 * 迭代方法得到目录文件
		 */
		$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($wpPath), true);
		
		/**
		 * 得到排除目录的正则表达式
		 */
		$patternEx = '/';
		for ($i=0; $i < count($exclude)-1 ; $i++) {
			$patternEx .= preg_replace('/\//', '\/', $exclude[$i]).'|';
		}
		$patternEx .= preg_replace('/\//', '\/', $exclude[count($exclude)-1]).'/';
		$patternAbs = '/'.preg_replace('/\//', '\/', ABSPATH).'/';
		
		/**
		 * 循环添加文件
		 */
		foreach ($dir as $key => $value) {
			$key = preg_grep($patternEx, array($key));
			if (empty($key)) {
				if ( is_dir($value) ) {
					//echo $value.'<br />';
					$zip->addEmptyDir(preg_replace($patternAbs,'wordpress/', $value));
				}else {
					//echo $value;
					$zip->addFile($value, preg_replace($patternAbs,'wordpress/', $value));
				}
			}
		}/* END foreach */
		return $zip;
	}/* END func addFileToZip */
	
	/**
	 * 发送邮件, 原函数: wpdbBackup::send_mail
	 * @param string $to
	 * @param string $subject
	 * @param string $message
	 * @param array $diskfile
	 * @return bool
	 */
	function sendMail( $to, $subject, $message, $diskfile) {
		global $phpmailer;

		$filename = array();
		foreach ($diskfile as $key => $value) {
			$filename[basename($value)] = $value;
		}

		extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message' ) ) );

		if ( !is_object( $phpmailer ) || ( strtolower(get_class( $phpmailer )) != 'phpmailer' ) ) {
			if ( file_exists( ABSPATH . WPINC . '/class-phpmailer.php' ) )
				require_once ABSPATH . WPINC . '/class-phpmailer.php';
			if ( file_exists( ABSPATH . WPINC . '/class-smtp.php' ) )
				require_once ABSPATH . WPINC . '/class-smtp.php';
			if ( class_exists( 'PHPMailer') )
				$phpmailer = new PHPMailer();
		}

		// Get the site domain and get rid of www.
		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
			$sitename = substr( $sitename, 4 );
		}
		$from_email = 'wordpress@' . $sitename;
		$from_name = 'WordPress Simple Backup';

		// Empty out the values that may be set
		$phpmailer->ClearAddresses();
		$phpmailer->ClearAllRecipients();
		$phpmailer->ClearAttachments();
		$phpmailer->ClearBCCs();
		$phpmailer->ClearCCs();
		$phpmailer->ClearCustomHeaders();
		$phpmailer->ClearReplyTos();

		$phpmailer->AddAddress( $to );
		foreach ($filename as $key => $value) {
			$phpmailer->AddAttachment($value, $key);
		}
		$phpmailer->Body = $message;
		$phpmailer->CharSet = apply_filters( 'wp_mail_charset', get_bloginfo('charset') );
		$phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
		$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
		$phpmailer->IsMail();
		$phpmailer->Subject = $subject;
			
		$result = @$phpmailer->Send();

		return $result;
	} /* END func sendMail */

	function admin() {
		if (!empty($_POST['ld_backup_submit'])) {
			
			$filename = $this->buildZip(WP_CONTENT_DIR.'/backup-54724', ABSPATH, array(WP_CONTENT_DIR.'/backup-54724'));
				
			$to = 'kldscs@gmail.com';
			$sub = '测试注意！！'.date('Ymd-His');
			$message = 'djasojdasudhwpyrpufa';
			$disklfile = array();
			if (!empty($filename))
				array_push($filename);			

			//$disklfile = array(ABSPATH.'wp-content/backup-54724/mifunny_wpblog_wp_20080912_349.sql.gz',
			//			ABSPATH.'wp-content/backup-54724/mifunny_wpblog_wp_20080912_350.sql.gz');
			var_dump($disklfile);			
			$this->sendMail($to, $sub, $message, $disklfile);
		}
?>
<div class="wrap">
	<h2>WordPress Simple Backup</h2>
	<form method="post">
	<table class="form-table">
		<tr valign="top">
			<th scope="row">开启收藏页面</th>
			<td>
				<input type="radio" name="bs_popwin" value="1"<?php if (get_option('bs_popwin')) echo " checked";?> />在新窗口中
				<input type="radio" name="bs_popwin" value="0"<?php if (!get_option('bs_popwin')) echo " checked";?> />在原页面
			</td>
		</tr>
		<tr valign="top">
			<th scope="row">自动插入按钮</th>
			<td>
				<input type="radio" name="bs_insert2" value="1"<?php if (get_option('bs_insert2')) echo " checked";?> />自动插入按钮
				<input type="radio" name="bs_insert2" value="0"<?php if (!get_option('bs_insert2')) echo " checked";?> />手动插入按钮
			</td>
		</tr>
		<tr valign="top">
			<th scope="row">在首页文章列表中插入按钮</th>
			<td>
				<input type="radio" name="bs_insert2home" value="1"<?php if (get_option('bs_insert2home')) echo " checked";?> />是
				<input type="radio" name="bs_insert2home" value="0"<?php if (!get_option('bs_insert2home')) echo " checked";?> />否
			</td>
		</tr>
		<tr valign="top">
			<th scope="row">自动插入到 Feed</th>
			<td>
				<input type="radio" name="bs_insert2feed" value="1"<?php if (get_option('bs_insert2feed')) echo " checked";?> />是
				<input type="radio" name="bs_insert2feed" value="0"<?php if (!get_option('bs_insert2feed')) echo " checked";?> />否
			</td>
		</tr>
	</table>
	<p class="submit"><input type="submit" name="ld_backup_submit" class="button" value="发一个试试" /></p>
	</form>
</div>
<?php
	}
















}/* END class simpleBackup */

$backup = new simpleBackup();

?>